package com.drinks.yiniang.ui.activity.user.invoice;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.drinks.yiniang.R;
import com.drinks.yiniang.ui.activity.common.WebActivity;
import com.drinks.yiniang.ui.fragment.user.invoice.InvoiceListFragment;
import com.drinks.yiniang.ui.fragment.user.invoice.InvoiceNotesFragment;
import com.drinks.yiniang.ui.fragment.user.invoice.InvoiceSubmitFragment;
import com.hansen.library.Constants;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;

/**
 * 电子发票页面
 *
 * @author han
 * @date 2019/11/08
 */
public class InvoiceManageActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        InvoiceNotesFragment.OnToMakeInvoiceListener, InvoiceListFragment.OnInvoiceListSureListener {

    //nav
    private NavigationBarLayout nav_bar_e_invoice;

    private InvoiceNotesFragment mInvoiceNotesFragment;
    private InvoiceListFragment mInvoiceListFragment;
    private InvoiceSubmitFragment mInvoiceSubmitFragment;

    private BaseFragment mCurrentFragment;

    @Override
    protected void initView() {
        nav_bar_e_invoice = findViewById(R.id.nav_bar_e_invoice);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        initFragment();
    }

    @Override
    protected void initListener() {
        nav_bar_e_invoice.setOnNavgationBarClickListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_invoice_manage;
    }

    @Override
    public void onBackClick(View v) {
        doFinish();
    }

    @Override
    public void onEditClick(View v) {
        if(mCurrentFragment == mInvoiceSubmitFragment) {
            Intent intent = new Intent(mContext, WebActivity.class);
            intent.putExtra(Constants.KeyType, WebActivity.WEB_TYPE_INVOICE_KNOWS);
            startActivity(intent);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            doFinish();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void doFinish() {
        if(mCurrentFragment == mInvoiceSubmitFragment) {
            toListPage();
        } else if(mCurrentFragment == mInvoiceListFragment) {
            toNotesPage();
        } else {
            finish();
        }
    }

    private void initFragment() {
        if (mInvoiceNotesFragment == null) {
            mInvoiceNotesFragment = InvoiceNotesFragment.newInstance();
        }

        nav_bar_e_invoice.setNavBarEditTextVisibility(false);

        mCurrentFragment = addFragment(mInvoiceNotesFragment, R.id.fl_invoice);
    }

    //开票须知页面
    private void toNotesPage() {
        if (mInvoiceNotesFragment == null) {
            mInvoiceNotesFragment = InvoiceNotesFragment.newInstance();
        }

        nav_bar_e_invoice.setNavBarEditTextVisibility(false);

        mCurrentFragment = switchFragment(mCurrentFragment, mInvoiceNotesFragment, R.id.fl_invoice);
    }

    //代开发票订单
    private void toListPage() {
        if (mInvoiceListFragment == null) {
            mInvoiceListFragment = InvoiceListFragment.newInstance();
        }

        nav_bar_e_invoice.setNavBarEditTextVisibility(false);

        mCurrentFragment = switchFragment(mCurrentFragment, mInvoiceListFragment, R.id.fl_invoice);
    }

    //提交发票
    private void toSubmitPage(String value) {
        if (mInvoiceSubmitFragment == null) {
            mInvoiceSubmitFragment = InvoiceSubmitFragment.newInstance(value);
        }

        nav_bar_e_invoice.setNavBarEditTextVisibility(true);
        nav_bar_e_invoice.setNavBarEditText(R.string.text_notes_for_invoice);

        mCurrentFragment = switchFragment(mCurrentFragment, mInvoiceSubmitFragment, R.id.fl_invoice);
    }

    @Override
    public void toMakeInvoice() {
        toListPage();
    }

    @Override
    public void onInvoiceSureClick(String value) {
        toSubmitPage(value);
    }
}
